package com.ChainBizPro.system.service.impl;

import com.ChainBizPro.system.common.ErrorCode;
import com.ChainBizPro.system.exception.BusinessException;
import com.ChainBizPro.system.mapper.SuppliersMapper;
import com.ChainBizPro.system.model.domain.Suppliers;
import com.ChainBizPro.system.model.dto.Suppliers.SuppliersQueryRequest;
import com.ChainBizPro.system.model.vo.SuppliersVO;
import com.ChainBizPro.system.service.SuppliersService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author liusijia
 * @description 针对表【suppliers(供应商)】的数据库操作Service实现
 * @createDate 2024-11-16 16:34:17
 */
@Service
public class SuppliersServiceImpl extends ServiceImpl<SuppliersMapper, Suppliers> implements SuppliersService {

    @Resource
    private SuppliersMapper suppliersMapper;

    @Override
    public SuppliersVO getSuppliersVO(Suppliers supplier) {
        if (supplier == null) {
            return null;
        }
        SuppliersVO suppliersVO = new SuppliersVO();
        BeanUtils.copyProperties(supplier, suppliersVO);
        return suppliersVO;
    }

    @Override
    public Page<SuppliersVO> getQueryWrapper(Page<SuppliersVO> pageParam, SuppliersQueryRequest suppliersQueryRequest) {
        // 自定义分页查询
        List<SuppliersVO> suppliersVOList = suppliersMapper.getQueryByAdmin(pageParam, suppliersQueryRequest);
        if (suppliersVOList == null) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "查询失败");
        }
        // 设置分页结果
        pageParam.setRecords(suppliersVOList);
        return pageParam;
    }

    @Override
    public Page<SuppliersVO> getQueryWrapperByMy(Page<SuppliersVO> pageParam, SuppliersQueryRequest suppliersQueryRequest, Long userId) {
        // 自定义分页查询
        List<SuppliersVO> suppliersVOList = suppliersMapper.getQueryByMy(pageParam, suppliersQueryRequest, userId);
        if (suppliersVOList == null) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "查询失败");
        }
        // 设置分页结果
        pageParam.setRecords(suppliersVOList);
        return pageParam;
    }

    private void addLikeCondition(QueryWrapper<Suppliers> queryWrapper, String column, String value) {
        if (!StringUtils.isEmpty(value)) {
            queryWrapper.like(column, value);
        }
    }
}




